/**
 * 
 */
package travibot.core;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import travibot.core.tags.Input;
import travibot.core.tags.LoginForm;
import travibot.core.tags.Input.InputType;


/**
 * @author tajzivit
 *
 */
public class LoginPageParser extends GenericParser implements Parser {

    private LoginForm loginForm;
    
    public LoginPageParser(){
        
        super();
    }
    
    public LoginForm getLoginForm(){
        
        parseRecursive(super.getDocument());
        
        return loginForm;
    }
    
    private void parseRecursive(Node node){
        
        String nodeName = node.getNodeName().toLowerCase();

        if(nodeName.equals("form")){
            parseForm(node);
        }
        
        Node child = node.getFirstChild();
        while (child != null) {
            
            parseRecursive(child);
            child = child.getNextSibling();
        }
    }
    
    private void parseForm(Node node){
        
        String nodeName = node.getNodeName().toLowerCase();
        NamedNodeMap attributes = node.getAttributes();
        
        if(nodeName.equals("form")){
            
            String name = attributes.getNamedItem("name").getNodeValue();
            String method = attributes.getNamedItem("method").getNodeValue();
            String action = attributes.getNamedItem("action").getNodeValue();

            loginForm = new LoginForm(method, name, action);
        }
        else if(nodeName.equals("input")){
            
            // class, type, name, value
            
            Node nameNode = attributes.getNamedItem("name");
            Node clazzNode = attributes.getNamedItem("class");
            Node typeNode = attributes.getNamedItem("type");
            Node valueNode = attributes.getNamedItem("value");
            
            String name = null;
            String clazz = null;
            String type =  "";
            String value = null;
            
            if(nameNode != null)
                name = nameNode.getNodeValue();

            if(clazzNode != null)
                clazz = clazzNode.getNodeValue();
            
            if(typeNode != null)
                type = typeNode.getNodeValue().toLowerCase();
            
            if(valueNode != null)
                value = valueNode.getNodeValue();
            
            if(Input.getInputType(type).equals(InputType.TEXT) && clazz != null && clazz.equals("fm fm110")){
                loginForm.setNameInput(new Input(clazz, type, name, value));
            }
            else if(Input.getInputType(type).equals(InputType.PASSWORD)){
                loginForm.setPasswordInput(new Input(clazz, type, name, value));
            }
            else{
                loginForm.addInput(new Input(clazz, type, name, value));
            }
        }
        
        Node child = node.getFirstChild();
        while (child != null) {
            
            parseForm(child);
            child = child.getNextSibling();
        }
    }
}
